home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Technotools
/
Technotools (Chestnut CD-ROM)(1993).ISO
/
batch
/
scanbat
/
tutr.doc
< prev
next >
Wrap
Text File
|
1989-10-02
|
110KB
|
2,436 lines
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
(C)opyright 1989 Paul Scanlon
Brought to you by Scanlon Enterprises
38354 17th St E # C
Palmdale, CA 93550
This tutorial is NOT an attempt to replace the MS/PC-DOS user
manual. This is an attempt to give instruction on Batch files in
general, and will advance to full and detailed explanations of
using our own utilities (SEBFU). We hope that this will be
informative and useful to you.
The material herein is written for Dos versions 2.x and above, if
you are using Dos 1.x many of the features described may NOT
function with your system. If you are using a Dos version lower
than 2.x, we heartily suggest that you upgrade. As a matter of
fact we suggest going to at least 3.2 . Dos has versions ranging
all the way thru 4.0 .
This document is copyrighted and may not be copied or otherwise
reproduced without written consent by the author. Doing so will
place the duplicator in direct violation of the Federal copyright
protection laws, and prosecution will be in federal courts.
If you find this document and our utilities useful and intend to
use them on a regular basis, you are required to register the
utilities. For details on registration read the REGISTER.DOC
file. Our fee for the utilities is only $19.50 (single user). We
feel that this price is very fair (and reasonable) and we will
support you. Our support includes answering any questions
regarding Batch files in general. Also, we want to know what
improvements to make, or what additional commands you would like.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 2
We will begin our discussion with a quick talk about Dos in
general. DOS, when used herein will refer to both PC-DOS and MS-
DOS. What is DOS ? DOS stands for Disk Operation System. Without
Dos, you couldn't easily save and retrieve data from disk drives.
Everything you enter at the DOS prompt is considered a command.
Commands are either internal or external. By internal, we mean
that the command will function if you have a formatted disk
(formatted using the /S option) which has the invisible files for
DOS and the visible file COMMAND.COM . External commands are any
legal file name with extensions of EXE, COM and BAT. Indeed,
COMMAND.COM itself is a command !
How does this relate to batch files ? Well, a batch file is
nothing more (nor less) than a file consisting of one or more
lines of commands. This is where the term batch file came from
(for batch processing). In other words, a batch file is simply
read by DOS, and each line therein is executed (if the line is a
command). Batch files are very useful, and can perform many
functions automatically, thus saving the user much time and
effort in entering key strokes for each function (command)
separately. One of the best know batch files is AUTOEXEC.BAT,
which performs setup information when you system is first booted.
We will now give a little more detail about Dos and its use. For
purposes of our discussion, we suggest that you make a formatted
disk using the /S option. To do this, enter :
FORMAT A: /S
Where A is the 1st floppy drive (in some cases the only one) on
your system. If you need more information on formatting a disk,
please consult your Dos manual.
Also, we suggest that you print this document, so that you can
read it while performing many of the suggested experiments and
tests. If you don't have a printer, we will send you a printed
copy for a small fee ($5), just mail us a check to the address on
page 1, and we will Airmail out a copy.
Now, if you have that disk ready, place it into drive A of your
system, and press the keys <CTRL>,<ALT> and <DEL> at the same
time. This will reboot your system. If you formatted the disk
correctly, you should see a message similar to the following.
Current date is Fri. 3-03-1980
Enter new date (mm-dd-yy):_
Press enter now, and you will then have the additional lines :
Current time is 9:37:12.05
Enter new time:_
Please press enter again (for our purposes, we don't need the
correct time and date.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 3
Your display should now be similar to the following :
Current date is Fri. 3-03-1980
Enter new date (mm-dd-yy):
Current time is 9:37:12.05
Enter new time:
Microsoft (R) MS-DOS (R) Version 3.20
(C) Copyright Microsoft Corp 1981-1987
A>_
Of course your time and dates and perhaps Dos version will be
different. Also if you are using PC-DOS, the copyright notice
will be different. Here we are, with a blank disk (except for
COMMAND.COM). Verify this, type DIR. You should have a screen
similar to the following :
Current date is Fri 3-03-1980
Enter new date (mm-dd-yy):
Current time is 9:37:12.05
Enter new time:
Microsoft (R) MS-DOS (R) Version 3.20
(C) Copyright Microsoft Corp 1981-1987
A>dir
Volume in drive A has no label
Directory of A:\
COMMAND COM 25276 3-03-1980 9:38a
1 File(s) 283648 bytes free
A>_
Notice that the line containing the word Volume ends with the
words no label. This means we didn't format the disk with the /V
option (read your Dos manual). If the disk had been formatted
with that option, the line would contain the label name. The
second line gives us the path of the directory, in this case, A:\
which is the root directory of drive (the only directory). And
finally the list of files (only one in this case). The list of
files has the file name (note the lack of the decimal point
separator between the 8 character name and the extension). This
line contains the file name (COMMAND.COM), the size of the file
in bytes (25276), the date the file was created (not necessarily
the date you formatted the disk) and finally the time the file
was created. The last line gives the total number of files in
the list, along with the number of bytes free on the disk.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 4
What does all this mean to you ? Well first off, we have
executed a command (DIR or dir) ! And we know that this command
is internal, because there isn't a file on the disk with the name
DIR.COM or DIR.EXE or even DIR.BAT . You ask, can the DIR
command be used in a batch file ? You betcha ! A batch file can
execute any command, be it an internal or external. And again,
the difference between external commands and internal ones is
that external commands require a file with the name used. IE..
when you formatted the disk, you used the command FORMAT. You
will find that FORMAT is a file with the extension COM. And yes,
format can be used from a batch file !
In fact, one of the batch files we will create, uses FORMAT. Why
are we spending so much time here ? Well, we want to make sure
you understand which commands are internal and which are
external. Here is a list of all of the internal commands :
BREAK CHDIR (CD) CLS COPY DATE DEL (ERASE)
DIR ECHO ERRORLEVEL EXIST EXIT
FOR GOTO IF MKDIR (MD) PATH
NOT PAUSE PROMPT REM RENAME (REN)
RMDIR (RD) SET SHIFT TIME TYPE VER
VERIFY VOL
And in Dos 3.3 we also have CALL and CHCP . Since these are new
to Dos, we won't be discussing them here, other than a brief
comment or two.
Some of the above commands can only be used from batch files. One
good example is the ERRORLEVEL command. The ERRORLEVEL is really
more of a flag than anything, and is a test of how a previous
command executed. Many programs don't set the ERRORLEVEL for use
in batch files, but you will find that our Batch File Utilities
make extensive use of it. More about ERRORLEVEL later.
Now, let's create our first batch file. We are going to create
an AUTOEXEC.BAT file. At this point, you should have a Dos
prompt of A>_ sitting in front of you. Enter the following :
COPY CON AUTOEXEC.BAT
Your screen should now have the line :
A>COPY CON AUTOEXEC.BAT_
Press enter. Your cursor will now move to the next line, but you
won't have a prompt. Don't get nervous, what we've done, is ask
Dos to copy a file, in this case the source file is the console
(or keyboard), the destination is the file AUTOEXEC.BAT . At
this time, we aren't going to put much into the file, so just
press the space bar once. The cursor will move to the right one
space. Now, hold down the control key (CNTL) and press the 'Z'
key. You'll see a ^Z on the display. Now press enter. You should
get your Dos prompt back (A>_) .
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 5
If you don't get your prompt back, try pressing the enter key
again. If you still don't have it, look at the screen. Do you
have the characters ^Z ? If not, press and hold the control key
then press the 'Z ' key again. Finally press enter. If you
still don't have the prompt, reboot your system, and start this
section over. If you just can't do it, let us know ! And for
now, just precede with the rest of this instruction.
We have now created a file called AUTOEXEC.BAT. It's time to
reboot your computer again. Leave the disk in drive A and press
<Ctrl> <Alt> <Del> (at the same time). Your system will reboot.
This time, you will not be greeted with all of the previous
information. You won't be prompted for the time or date, and
your screen will look similar to the following :
A>
A>
What, no messages ? !! Yep, you got it. By creating a file with
a single space in it, we managed to by pass all those messages.
What good is that ? Well, if you have an AT class machine or
some other type that has a built in time clock, you can by pass
those opening messages and get on to what you want to do. Of
course, there are much more important uses for AUTOEXEC.BAT than
this. For instance, what about that silly prompt ? Can that be
changed ? The answer is yes.
How do you do that ? Well we use the PROMPT command of course !
OK, enter the following on your command line. (A brief note, when
ever we say command line we are referring to the current line
with the Dos prompt on it.) Enter PROMPT $g and press enter.
Your display should have :
A>PROMPT $g
>_
Whoops, we lost the drive letter, how do we know where we are ?
Enter 'dir' and press enter (>dir_<enter>)
Your directory list will now contain 2 files, COMMAND.COM and
AUTOEXEC.BAT . Well that's one way to find out what drive you're
on. Back to the prompt command. Enter PROMPT $P$G
>PROMPT $P$G_
Now press enter. Look what you got now.
A:\>_
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 6
What we've done is tell Dos to display a prompt using the path
(in this case A:\ is the path) and the greater than symbol. For
more details on use of the PROMPT command, refer to your Dos user
manual. It is not our intent to teach you about Dos, but we will
be giving some information as it pertains to batch files.
Earlier we mention that AT's and similar machines don't require
the user to enter the time and date when their system is booted.
This also applies to those who have PC's with real time clocks
added to them. Usually these systems have a command similar to :
TIMER /I
appended to the AUTOEXEC batch file. This command when run at
boot time sets the Dos time and date from the real time clock.
For those of you who have this, we refer you to the documentation
which came with your system or when you had the clock installed.
We will cover this topic later on, and show you where in the
AUTOEXEC batch file to place your command for the real time
clock. For those who have AT's and 386 machines, the time and
date is automatically set at boot time from your Rom Bios.
OK, now let's add the PROMPT command to our AUTOEXEC batch file.
At the command line, enter COPY CON AUTOEXEC.BAT then press
enter. You won't have a Dos prompt, the cursor will be on the
next line, IE....
A:\>COPY CON AUTOEXEC.BAT
_
Enter the string 'PROMPT $P$G' and press enter. Your display
will now have these lines :
A:\>COPY CON AUTOEXEC.BAT
PROMPT $P$G
_
Press and hold the control key, then press the letter 'Z', a ^Z
will appear on the screen, release the control key and press
enter. You should get the message '1 file copied', and then your
Dos prompt on the next line.
A:\>COPY CON AUTOEXEC.BAT
PROMPT $P$G
^Z
1 File(s) copied
A:\>_
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 7
Reboot your system. This time when it boots, you will get the
following screen.
A>PROMPT $P$G
A:\>
A:\>_
Now why did the line 'PROMPT $P$G' show up ? A little more about
Dos is in order. Whenever Dos runs a batch file, the Echo mode
will be on. What this means, is that all commands in the batch
file will not only execute, but will also be echoed to the
display. What can we do about it ? Let's modify our AUTOEXEC
batch file again. Enter the command line 'COPY CON AUTOEXEC.BAT'
and press enter. On the first line, enter 'ECHO OFF' and press
enter. The next line, enter 'PROMPT $P$G' and enter. Now press
and hold the control key, press the letter 'Z', release the
control key and press enter. You should now have a display
similar to this :
A:\>COPY CON AUTOEXEC.BAT
ECHO OFF
PROMPT $P$G
^Z
1 File(s) copied
A:\>_
OK, reboot again. If all this booting is making you nervous,
don't worry, we won't be doing it much longer. We just want to
give you an idea of what happens when ever you change something
in the AUTOEXEC batch file.
After rebooting, your display should look something like :
A>ECHO OFF
A:\>
A:\>_
We still have the command echo on the display. Let's now go thru
and see exactly what we did. The first line is the echo command,
followed by the switch OFF. What this means, is that if the echo
command is followed by a single word ON or OFF, then the echo
mode will be switched on or off. Just to get an idea of what we
are talking about, at the command line enter 'echo Hello, I'm
here.' and press enter. You will see:
A:\>echo Hello, I'm here.
Hello, I'm here.
A:\>_
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 8
One noticeable quirk of the echo command with the on and off
switches, is that if at the command line you enter ECHO OFF, you
will lose you prompt. Try it. To regain the prompt, just enter
ECHO ON at the next line (where ever your cursor is).
Onward. Well, we've now learned 2 commands we can use in batch
files. Although we are using the AUTOEXEC.BAT file for our
purposes, these same commands can work in any file. IE, try the
following command sequence.
A:\>COPY CON TEST.BAT
ECHO OFF
CLS
PROMPT $P$G
^Z [ENTER]
A:\>_
Enter Dir at the Dos prompt. You will now have 3 files listed,
COMMAND.COM, AUTOEXEC.BAT and TEST.BAT. Now type TEST on the
command line. Your display should clear and your Dos prompt will
be at the top of the screen. We've added a new command, CLS. The
CLS command clears the screen, and positions the cursor to the
very top of the display.
Let's talk a little more about Dos. If we were to keep adding
files to our disk (like we're doing), we would soon have a full
disk. MS-DOS allows us to have only a little over 100 files in
any directory.
What is a directory ? A directory can be considered much like a
file cabinet, which has many doors (or drawers), which when
opened, presents either more doors or some files. In this case,
the files are your programs (like AUTOEXEC, TEST and COMMAND),and
the doors are directories. The root directory is the starting
point for all other directories, much like the lobby of a large
building.
You can precede thru directories in 2 directions, up or down.
When going up, you are moving away from the root directory, when
going down, you are moving toward the root directory. You can
view this motion much like an elevator ride. Take the elevator up
and get off at a floor to see what's there.
Now that we've given you some input on what a directory is, let's
make one. To make a directory, Dos has the command MKDIR, or the
2 character version MD, Dos will perform the task (make a
directory) no matter which you enter. Enter 'MD BATCH' and press
enter. All that appears to have happened is that you've gotten a
new Dos prompt. Enter 'dir' at the command line. You should have
a list similar to the following :
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 9
A:\>dir
Volume in drive A has no label
Directory of A:\
COMMAND COM 25276 7-24-87 12:00a
AUTOEXEC BAT 13 3-03-89 9:45a
TEST BAT 28 3-03-89 9:55a
BATCH <DIR> 3-03-89 10:01a
4 File(s) 280576 bytes free
A:\>_
Notice that we are using up disk space quickly. Earlier we had
283648 bytes free (when we first issued the dir command with only
COMMAND.COM on the disk). No matter how small a file is, Dos
allocates 1K (1000) bytes to it. This allocation is called a
block, and is the smallest amount of room any file may occupy on
a disk. A block is larger on a hard disk (usually 2000 bytes).
Even so, taking our initial value of 283648 bytes, and dividing
by 1000, we would come up with about 283. This is the most files
we can get on a standard floppy disk formatted with the /S
option. If we had not used the /S option, there would have been
362000 bytes free when we started, which translates to 362 files.
As can be seen from the above, even if we are using a small media
like a floppy disk, we can have several hundred files on a disk.
That is, we could if Dos let us. As I earlier, Dos permits only
about 100 files per directory. The clue here is the word
directory, and that's how we get past the 100 file barrier. By
making a directory, we add the capability of adding an additional
100 files. Only thing is, we lost one, that is, the directory
entry itself is counted in the root directory. But even so, who
cares, we added over 100 more entry possibilities, so what do we
care if we lose one.
Of course, each entry in the root directory could be a directory
entry. What that translates to is 100 x 100 or 10,000 ! So if we
created 100 directories, we could put 10,000 files on the disk.
Whoops, in actuality, we can't ! Why ? Because we will run out
of physical room ! If each file uses up 1000 bytes each, and we
started out with only 283000, then we have room for only the 283
files, and that includes the directory entries themselves. IE...
when we created the Directory BATCH, we used up another 1000
bytes.
The above holds true for the standard 360K floppy disks, what
about the AT 1.2meg (1,200,000 bytes) disks ? Again each file
uses 1000 bytes, so 1.2 million divided by 1000 is 1200 files !
Still not really a whole lot. Now what of hard disks ? Each file
takes up 2000 bytes (a little more waste there). A popular hard
disk is the 20 meg unit, usually like a Segate ST210, which can
hold over 20 million bytes. 20 million divided by 2000 is 10,000.
Still not a lot of files, but we are getting there.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 10
Many small business are using 80 and 120 meg units. A 120 meg
unit holds 120 million bytes, so, at 2000 bytes per file, the
drive could potentially hold 60,000 files ! Many users are now
talking about gigabyte drives, this relates to billions of bytes.
If a drive held one giga byte (1 billion bytes), then the drive
could hold a half million files !
Of course the above numbers assume that all files would be in the
range of 1 thru 1000 bytes (floppies) or 1 thru 2000 bytes (hard
disks). This number can shrink quickly for those having files
that consume several hundred thousand bytes each or some files
that consume more than a million bytes.
Enough of this, lets consider the case where we have lots of
small files. If each file was less than 1000 bytes, and we had a
standard floppy system (360k bytes each disk), then we could
potentially get 360 files on the disk. This is where your
directories come in. Could you imagine wading thru 300 plus files
each time you wanted to find something ? Well, that's exactly
what Dos would have to do if we could only use one directory.
When you enter a command, that isn't one of COMMAND.COM 's
internal ones, then Dos has to find the file related to that
command. How does Dos look for this file ? Dos looks first in
the current directory, first for a file with the extension COM,
then with the extension EXE, and finally with the extension BAT.
If Dos can't find the file in the current directory, it would
start looking in other directories, names of which it found in
the PATH statement.
By this time, you might be wondering if all of this is necessary,
well, it is, and we are leading up to something. Anyway, what is
this PATH statement (command). First, we have to tell you about
another internal command called SET.
Enter the following on the command line : 'set' then press enter.
You should see the following on you display.
A:\>set
PATH=
COMSPEC=A:\COMMAND.COM
PROMPT=$p$g
A:\>_
The 1st 2 entries are always there. PROMPT=$p$g is a result of
our autoexec batch file and the line containing PROMPT $p$g. This
information is in a space, reserved by Dos, called the
environment space. Dos sets aside 160 bytes for this
information, IE... PATH= is 5 bytes, COMSPEC=A:\COMMAND.COM uses
22 bytes, etc... In fact, in the above entry, we have used a
total of 42 bytes of the environment space.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 11
If you counted all of the characters above, you'll find that
there are only 38 characters shown. The remaining 4 bytes are
used by Dos, 2 bytes to separate the entries (1 byte between each
one, which is 2), and 2 bytes to mark the end. Don't worry about
what byte Dos uses to mark the end, let Dos worry about it. The
purpose in telling you this is let you know how much space you
have and that you shouldn't waste it.
Now, how can you waste the space ? Well, the 3 entries in the
environment space are not all that are place there. As we
progress thru this tutorial, we will discuss more entries, as
well as using up more space for the ones we have, which brings us
back to our PATH statement (command).
You will note, above, that the PATH statement had no parameters
after it (PATH= was on a line all by itself). This means, that
Dos will look no further than the current directory for commands.
Now, lets test this out. Enter 'CD BATCH' on the command line,
followed by pressing the enter key. Your Dos prompt should now
look like the following.
A:\BATCH>_
Enter the following 'PROMPT' and press enter. You will now have
the following prompt.
A>_
We're back to the root directory ? No, we have just told dos NOT
to inform us of our location. Without the other information how
would we know where we are ? Enter 'dir' and press enter. Well,
that's one way. The directory command always lets you know where
you are. Of course that's awfully inconvenient. OK, let's get
back our prompt, enter 'prompt $p$g' and press enter. Here we are
again with our directory name.
Earlier, we just entered the name TEST to run the batch file we
created earlier, called TEST.BAT . Let's try it again, enter
'TEST' and press enter. You got the following message.
Bad command or file name
Well, that's how Dos lets you know that it couldn't find the file
TEST.BAT . So how do we execute a program (command) from this
directory ? From this directory, you would have to specify where
the file was, as in the following 'A:\TEST' and press enter. By
prefixing TEST with the A:\ , we have told Dos to look in the
root directory to find the file TEST.BAT . Go ahead and try it.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 12
That's where the PATH command comes in. We can have Dos search
more than the current directory for a file by entering a search
path in the PATH statement. Enter 'PATH=A:\' and press enter.
Now enter 'set' and press enter. Look at that, the 1st line now
has the sequence 'PATH=A:\'. Now we try out our added path. You
should still be in the BATCH directory, and have the prompt
A:\BATCH . Enter 'test' on the command line and press enter. The
batch file TEST should have executed, clearing your screen and
moving the cursor to the top of the display.
As can be seen, by setting the PATH to include the root
directory, we can run a file that exists in other than the
current directory. This will hold true no matter which directory
you had placed your file(s). Let's try another example. Enter
'cd ..' (yes, two dots after the cd) and press enter. You are
back at the root directory as depicted by you prompt of 'A:\>_'.
We found a way to go back one directory level, and only one
level. IE... if you were in a directory 2 levels up,
(A:\BATCH\GAME for instance), and entered the previous command,
you would be brought back one level (to A:\BATCH).
If you had been 2 levels up (A:\BATCH\GAME), and entered 'CD \'
instead of 'CD ..' you would have been taken all the way back to
the root directory. Remember, if you want to go all the way back
to the root directory, just enter 'CD \'.
Onward ! Enter 'dir' and press enter. You should see all of the
following files listed.
COMMAND.COM
AUTOEXEC.BAT
TEST.BAT
Remember, that when you use the DIR function, that the list does
not include the decimal point in the file names, IE...
COMMAND.COM would be COMMAND COM . Enter 'COPY *.BAT BATCH' and
press enter. We've just used another command, the COPY command.
Again, it's an internal command. How do we know this ? Because,
we don't have a file on this disk called COPY, nor does a file
called copy exist in the directory BATCH. If everything went
correctly , your display should have a line, just above the
current command line, which says '2 File(s) copied' . Enter the
command 'cd batch' on the current command line, then press enter.
Again, we are in the directory called BATCH, and we know this
because the prompt is 'A:\BATCH>_' . Now enter 'path=' and press
enter. We have just erased our path, verify this by entering
'set' and pressing enter. The list should include a line 'PATH='
with nothing after the equal sign. This means, that our search
for a file will only occur in the current directory. Enter
'TEST' on the current command line and press enter. The screen
will clear, and the prompt will be displayed on the 1st line.
Enter 'cd \' and press enter. We are back at the root directory.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 13
Now, we have 2 copies of the files AUTOEXEC.BAT and TEST.BAT, one
in the current directory (root), and one in the directory BATCH .
Let's erase one of these files. Enter 'ERASE TEST.BAT' and press
enter. Enter 'dir' and press enter. You will see that we now
only have the entries :
COMMAND COM
AUTOEXEC BAT
BATCH <DIR>
Now, let's discuss what we've done. We've used the COPY command
to duplicate the files AUTOEXEC.BAT and TEST.BAT and place them
into the directory BATCH. We've now used the COPY command several
times (the first being when we created the AUTOEXEC batch file),
so we should give you some information as to what it is doing.
The copy command is used to copy files. Your command line for
copy includes the command COPY, the source file, and finally the
destination file. In our first use, the source file was the
keyboard (our typing on the keyboard was the data), in the second
case, the source files were the files AUTOEXEC.BAT and TEST.BAT
on drive A, and in the current directory (the root). The
destination in our first use was AUTOEXEC.BAT on drive A in the
current directory (the root), while the second use placed the
destination in the directory BATCH.
Our second use also excluded entering the actual file names in
the destination and not fully specifying the source files. For
further information on usage of the COPY command, we refer you to
the Dos user manual. Since the purpose here is to direct you in
the use of batch files, and NOT the actual use of commands,
except where that explanation will enhance the explanation of
that use in the batch file, we suggest, that if you haven't
already done so, that you read you Dos manual, and take special
note of the commands mentioned on page 4.
Our new command is ERASE. You might note, that from now on, we
will be using the short version of this command 'DEL'. Both
forms of the command do exactly the same thing. Dos has short
versions of many of the internal commands, such as 'MD' for MKDIR
and 'CD' for CHDIR. The command CD changes the current directory
to the one specified, as we did when we entered 'CD \', while MD
creates a directory as we did quite a bit back, when we entered
MD BATCH on page 8.
Now lets again demonstrate the use of PATH. Enter 'test' and
press enter. You will get the message 'Bad command or file name'
message. Why ? Because it's no longer in our current directory.
Enter 'dir' and press enter. See, no TEST.BAT . Now we can set
the path so that Dos can find TEST. Enter 'path=batch' and press
enter. Enter 'set' and press enter. One of the lines display
should now read 'PATH=batch'.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 14
We are ready to test our search capability. Enter 'test' and
press enter. The screen cleared and the prompt is at the top of
the display. The foregoing was a very small example of how to
use path, and we will be giving more uses as we progress thru our
creation of the AUTOEXEC.BAT file.
What else do we want to do when we first boot our machine ?
Well, let's see what we can do so far. We clear the screen (to
make sure we don't have a bunch of junk on it. AUTOEXEC is
started with the first line ECHO OFF, then clears the display
with CLS, and finally we set the prompt we want to use with the
prompt command, PROMPT $P$G . Lets rewrite the TEST.BAT file to
include the path command. Enter the following lines.
A:\>COPY CON TEST.BAT
ECHO OFF
CLS
PROMPT $P$G
PATH=BATCH
^Z
Dos responds with '1 file(s) copied'. And don't forget, that the
'^Z' in the above is when you hold the control key down and press
the 'z' key. Also, you must have pressed enter after each line.
Ok, let's test it, enter 'test' on the command line and press
enter. The screen clears, and a prompt is placed at the top of
the display. We now have a batch file that when executed, will
turn the echo mode off, clear the display, position the cursor at
the top of the screen, set our prompt to show the current drive
and path, and finally set the search path to include the
directory BATCH . What else can we do ? Before we continue with
more uses of the batch file, we must back track for a second.
Previously, we showed the Dos environment by entering the command
'set' at the Dos prompt. Do it now. Note the line that contains
'COMSPEC=A:\COMMAND.COM' . This line lets Dos know where to find
the program 'COMMAND.COM '. When ever an application executes,
and finally concludes, you are returned to the Dos prompt. What
is the Dos prompt ? The Dos prompt is really a program that is
executing, getting and interpreting your keyboard entries, thus
performing selected tasks. The program doing this feat is
COMMAND.COM . Thus, when another program is thru, Dos must
reload and start COMMAND.COM again. To do this, it must know
where to find it, thus the string COMSPEC=A:\COMMAND.COM in the
Dos environment. If this string contained for instance
'COMSPEC=A:\BATCH\COMMAND.COM', then Dos would try to find
COMMAND.COM in the directory BATCH .
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 15
If after executing a program, Dos found a different disk in drive
A, and that disk did not contain COMMAND.COM in the root
directory, then the system would halt, you would get a prompt to
place a disk containing COMMAND.COM in drive A and press any key
to continue. Dos would not let you enter any new commands until
it had reloaded COMMAND.COM. So why are we telling you this ?
Now we can give you the next line for your AUTOEXEC.BAT file,
currently called TEST.BAT . We could enter a string
'COMSPEC=B:\COMMAND.COM ' or 'COMSPEC=A:\BATCH\COMMAND.COM ' in
our batch file, so that when executed, the new path for
COMMAND.COM would be set to B:\ (the root directory of drive B)
or to A:\BATCH (the directory BATCH on drive A).
Our next line in the batch file then becomes
'COMSPEC=A:\COMMAND.COM ' . Now we have the five lines as
follows :
ECHO OFF
CLS
PROMPT $P$G
PATH=BATCH
COMSPEC=A:\COMMAND.COM
Go ahead and use the COPY command to change the file TEST.BAT to
the above lines.
One of the biggest problems with batch files, is that they can't
key keyboard responses, other than the one used when the command
PAUSE is encountered. When PAUSE is encountered in a batch file,
the execution of the batch file halts until the user presses any
key. Alternately, the user can press ^C (control C) to halt the
batch file completely. How can we use PAUSE ?
During execution of the AUTOEXEC batch file, we might include a
line such as the following.
ECHO If you do not want enter the wordprocessor mode, press ^C
now, else
Note, that the line would only be echoed on a single line (we can
have 80 characters per line), and that else is NOT followed by
any other character or punctuation. This is because the line
following the ECHO command will be the PAUSE command.
Execution of the PAUSE command produces a message, on the line
following the message in the ECHO command, which is 'Strike a key
when ready . . .' . Dos would then wait for the user to press any
key to continue execution of the batch file, or would exit the
batch file if the user entered ^C.
Once the user entered ^C, Dos would display the following prompt:
Terminate batch job (Y/N)?_
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 16
Dos would then wait for the user to enter a 'y' or 'n' in
response. The response must be Y or N, or the prompt is reissued.
If the user enters Y, then the batch file execution is halted,
and the user is given the Dos prompt. However should the user
enter N, Dos would continue the batch file process starting on
the line following the PAUSE command. The batch file process
also continues on the line following the PAUSE command if the
user had pressed any key other than ^C .
What comes after the PAUSE command ? In our example, we can
enter the following lines.
CD EDITOR
EDLIN
Our batch file now contains the following lines
ECHO OFF
CLS
PROMPT $P$G
PATH=BATCH
ECHO If you do not want enter the wordprocessor mode, press ^C
now, else
CD EDITOR
EDLIN
Of course, we don't have a directory called EDITOR, yet. Go ahead
and change your TEST.BAT file to the above.
Now that we are actually doing something that will not be
transparent. Please notice the last line in our batch file,
which just has the word EDLIN . This is one of the files which
comes with Dos. The full name, including the extension, is
EDLIN.COM . You are probably already ahead of me here, and have
the disk with EDLIN on it in hand, if not, find it now. This
will be on one of you Dos disks.
We assume you have the disk with EDLIN on it ready. First thing
we have to do, is create the directory EDITOR on our disk. From
now on, we will call the disk we made, the work disk. If you
aren't already at the root directory on drive A, get there now.
You are now on drive A and in the root directory. On the command
line, enter 'MD EDITOR' and press enter. As stated earlier, this
creates the directory EDITOR . Now, change directories using the
following : cd editor
You should now have a Dos prompt like this :
A:\EDITOR>_
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 17
For those of you with 2 drives, perform the following steps.
Place your disk with EDLIN on it into drive B (close the door).
On the command line enter 'COPY B:EDLIN.COM ' and press enter.
Dos will copy the file EDLIN.COM from drive B onto drive A and
respond with '1 file(s) copied ' . You now have EDLIN on our work
disk. Remove the disk in drive B and restore it.
For those of you with only 1 drive, perform the following steps.
(Those with 2 drives should bypass the follow)
On the command line enter 'COPY B:EDLIN.COM ' and press enter.
(Yes, we know that you don't have an actual drive B, Dos will use
drive A for both A and B .)
Dos will respond with the message :
Insert diskette for drive B: and strike
any key when ready
Remove our work disk, and insert the Dos disk with EDLIN on it,
and press enter (any key will work), and Dos will eventually
respond with :
Insert diskette for drive A: and strike
any key when ready
Remove the Dos disk and insert our work disk, then press enter.
Dos will respond with :
1 File(s) copied
You now have EDLIN on your work disk (regardless of which method
you used). Perhaps you've been wondering why we have been
creating TEST.BAT each time we want to add something to it. It
has been our experience that some users are unaware of this
particular use, and we wanted you to have the experience of using
this method. Using the 'COPY CON filename ' approach is
especially good for creating small batch files, however, once
created, it can be a drag constantly changing an existing one as
we are doing.
From now on, it will be up to you, the user to edit or change the
batch file TEST, in whatever fashion you prefer. We will not, in
this document, teach you how to use EDLIN, if you want to learn
how to use it, we suggest that you pull out your Dos manual and
start experimenting with EDLIN before you precede with this
document, as we will be directing you, continually to change the
file TEST, and we will be creating new batch files as well.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 18
For the time being, we will cease our explanation of the AUTOEXEC
batch file and begin the process of learning other useful Dos
commands to be used with batch files in general.
Earlier, we used the DIR command from the Dos prompt (command
line), now we are going to add to your knowledge of its use. At
your current Dos prompt enter 'dir' and press enter. You should
have a list similar to the following :
Volume in drive A has no label
Directory of A:\
COMMAND COM 25276 3-03-1980 9:38a
AUTOEXEC BAT 13 3-03-1987 8:00a
TEST BAT 28 3-03-1987 8:15a
BATCH <DIR> 3-03-1987 8:30a
EDITOR <DIR> 3-03-1987 8:45a
5 File(s) 271360 bytes free
A file entry consists of first, up to 8 characters for the file
name, a space and 3 characters for the extension, a few more
spaces, then the file size (how many bytes it has), a few more
spaces, then the date the file was created (this may not be the
date you placed it on your disk, as is the case with
COMMAND.COM), then a few more spaces separate the last entry
which is the time of day the file was created (again, this may
not be the time of day you copied it to the disk as with
COMMAND.COM). As observed in the above list, COMMAND.COM has a
creation date (yours may differ) of 3-03-1980 and a time of 9:38a
which was the time that file was created (perhaps by Microsoft).
The date we copied the file to our disk, using FORMAT with the /S
option, does NOT apply. IE.... if you have a file called XYZ.COM
with a creation date of 3-05-1982 and copy it to another disk,
that file on the new disk will have the same date. However, if
you modify file XYZ.COM on the new disk, and resave it, then that
file will get a new date, equal to the date you made the
modification.
Now, look at the directory list again, and notice the last 2
entries. These are directory entries. A directory entry has a
name (and may also have an extension), then will have an entry of
<DIR> in the size field (directory names don't have a byte
count), and the last 2 fields are the same as that used for
files, IE... the creation date and time.
The directory command can be append with one of two switches
(they can not be used at the same time). The first one is to
append the switch for pause. This is often called page. The use
of this command is 'dir /p '. What does this form do ? Well, if
we had more files on our disk (about 22) the screen would pause
it's scroll when it got full. Since we don't have enough files
to demonstrate it, we'll go on to the next switch /w.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 19
The /w switch, when appended after the dir command causes the
directory list to use 5 columns, and drop off the extra
information (file size and time and date of creation). Try the
following at your Dos prompt. Enter 'dir /w ' then press enter.
Your should have a display similar to the following :
Volume in drive A has no label
Directory of A:\
COMMAND COM AUTOEXEC BAT TEST BAT BATCH EDITOR
5 File(s) 271360 bytes free
All of the files are listed starting from the left, with 5 files
per line. If we had more files, there would be 2 lines or more.
Another popular use of the AUTOEXEC batch file is to have the
batch file display a directory listing as the last line to be
executed in the batch file.
EXAMPLE:
ECHO OFF
CLS
PROMPT $P$G
PATH=BATCH
DIR /W
Upon booting a system with this as an AUTOEXEC batch file, the
screen would clear, and a directory list (similar to that above)
would be displayed, and the Dos prompt would follow.
There is another batch file command which can be important in
your AUTOEXEC batch file, and that is the BREAK command. The
break command is followed by one of the words ON or OFF. For
instance, the command BREAK ON sets the system to respond to the
^C sequence more often than the command BREAK OFF . For more
details, we refer you to your Dos manual. Our suggestion is to
include the command BREAK ON in your AUTOEXEC batch file, and if
you need to turn ^C testing off, you can always put the line
BREAK OFF into another batch file.
Another very popular use of the AUTOEXEC batch file is to
autostart an application (or game) when the disk is booted. An
example of this would be :
ECHO OFF
CLS
PINBALL
AUTOEXEC
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 20
The above batch file would continually run the program PINBALL .
The only way out of the loop would be to press ^C at the right
moment of time to halt the batch file. The last line, calls the
AUTOEXEC batch file again. A similar approach would be to use a
label, as in :
ECHO OFF
:START
CLS
PINBALL
GOTO START
Again, we have a batch file that will continually execute itself,
but this time, the loop is internal. When Dos gets to the last
line, it jumps back to the line with :START on it, and loops back
thru. This type of AUTOEXEC is great for disks that have only one
program on it, however, if you had one with several programs, and
you wanted to execute a particular one first, then the following
would be the batch file to use :
ECHO OFF
CLS
PROMPT $P$G
PATH BATCH
MENU
The last line contains the name of the program you want to run
first. In this case, it's a menu. This menu could be one of many
that are available in the Public Domain, or it could be our GMENU
program. A menu (like our GMENU) will display a list of other
programs, wait for you to select one, then execute that program,
when done with the selected program, you will be returned back to
the menu (as with our GMENU) or maybe back to Dos.
Here's another good example of using the AUTOEXEC batch file.
ECHO OFF
CLS
PATH BATCH
ECHO Have you backed up lately ?
PROMPT The current date is $d$_
PROMPT The current time is : $t$_
PROMPT $P$G
DIR /W
This little number will first display the line 'Have you backed
up lately ? ' then the line 'The current date is 3-03-87 ' and
finally the line 'The current time is 8:45 ', followed by a
directory list and the Dos prompt. Notice that the first two
lines that begin with PROMPT end with a dollar sign and an
underline. This sequence forces the prompt to issue a carriage
return. Without this sequence, the first two lines would be on
the same line.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 21
Why did we put the messages 'The current date is ' and 'The
current time is ' into the prompt string ? If we had used ECHO
to display the strings, then the strings would be on one line,
while the time or date would be on another. In other words, the
ECHO command always issues a carriage return.
For those without real time clocks in their systems, the
following AUTOEXEC batch files has proven useful.
ECHO OFF
CLS
PATH=BATCH
TIME
DATE
PROMPT $P$G
ECHO Have you backed up lately ?
DIR /W
In this example, we get Dos to prompt the user for the time and
date. In both this example and the previous example, we have the
line 'ECHO Have you backed up lately ? '. We suggest that you
keep back up copies of all your original disks, and also backups
of all work disks. This is so that in case any thing happened to
the current disk, you will always have a back up (or spare). It
will only take one time of loosing a disk to make you a believer!
Let's try a new command ! Actually we are going to test a couple
of new commands. Create a new TEST.BAT file with the following
lines.
ECHO OFF
CLS
IF EXIST HELP\HELP.COM GOTO GHELP
ECHO Help is NOT available
GOTO EXIT
:GHELP
ECHO Help IS available
:EXIT
Execute the file by entering 'test' at the Dos prompt. Your
screen will clear, the message 'Help is NOT available ' will be
displayed, and you will be given the Dos prompt.
Now lets create a directory called HELP. Enter the command 'md
help'. Now enter into the newly created directory by entering
the command 'cd help'. You should now have a dos prompt of
'A:\HELP>_' . Now we need to create the file HELP.COM . Do that
by entering the command 'COPY ..\COMMAND.COM HELP.COM ' (don't
forget to press enter after each of these commands). Dos will
respond with '1 File(s) copied' and give you another prompt. We
are now ready to try out the batch file again.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 22
Enter 'cd \' at the Dos prompt, you will now be in the root
directory. Enter the following from the Dos prompt, 'test' and
press enter. If we've done every thing right, the following
message will be displayed after the display is cleared.
Help IS available
followed by the Dos prompt. Of course, we don't really have a
HELP program, all we have is a renamed COMMAND.COM file. Now,
let's clear this directory and try something else. Perform the
following steps.
Enter 'cd help' and press enter
Enter 'del help.com' and press enter
Enter 'cd \' and press enter
Enter 'rd help' and press enter
Enter 'dir' and press enter
The directory listing should be like that on page 18, and your
prompt should be 'A:\>_ '.
Look at the lines for the TEST.BAT file on page 21. Notice the
3rd line, this is where all the new stuff is happening. The first
time we ran the batch file, the directory HELP had not been
created, nor had the file HELP.COM . That 3rd line tests for the
existence (IF EXIST) of HELP.COM in the directory HELP, and if it
does NOT exist, will execute the line immediately after that
line, which says 'Help is NOT available', then branches to the
line :EXIT . If the file HELP.COM does exist in directory HELP
then the program branches to the line :GHELP and displays the
message 'Help IS available. Now we've used GOTO, IF and EXIST.
These 3 commands are often used together.
By now, you may have guessed, that in actuality, that writing
batch files is a form of programming ! One of the big advantages
of writing programs using a batch file, is that the language can
grow to meet your needs.
Perhaps you've guessed why we have used the IF EXIST feature. Our
test for the existence of HELP.COM is a common occurrence of many
batch files written by professional programmers. IE... if the
file did not exist, we might have exited the batch file with a
warning message, asking the user to make sure that the file
HELP.COM is copied on to the disk, or we might have looked on
another drive, or we might have waited for the user to insert
another disk. On the other hand if the file had been found,
perhaps we would have continued on an executed the program HELP .
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 23
Another very powerful feature of using batch files, and which
seems seldom to be used, is the FOR %F IN()DO command sequence
These commands are used together. Lets take a look at what be a
typical use of this powerful command (typical is used for lack of
a better definition). If a batch file contained the following
line:
FOR %%F IN (%1, %2, %3, %4) DO DEL %%F
We could delete 4 unique files at one time, by entering one
command with all of the files named on it. Let's call this file
D.BAT . A command we might give would be D COMMAND.COM FILE.1
TEST.BAT TEST.DAT . Executing the command, we would see on the
display the following sequences of commands.
A:\>FOR %F IN (COMMAND.COM FILE.1 TEST.BAT TEST.DAT) DO DEL %F
A:\>DEL COMMAND.COM
A:\>DEL FILE.1
A:\>DEL TEST.BAT
A:\>DEL TEST.DAT
Of course we could have made the procedure invisible by entering
the following commands in the file, instead of the one line.
ECHO OFF
CLS
FOR %%F IN (%1, %2, %3, %4) DO DEL %%F
There it is, a real honest to God program that does something
useful. So how do we use this ? As we mentioned much earlier,
the path command points to alternate drives and directories for
Dos to search for a given command. Also, we have, on our work
disk, set the PATH to include the directory BATCH on drive A .
What you would do, is place this new batch file in the directory
A:\BATCH and then no matter which directory you might be in, you
could enter 'D ' and a string of files you want to delete.
Let's improve on a powerful utility. Yes, this can definitely be
called a utility. You may ask how can this be improved ? Well,
I'll borrow an age old adage, 'Anything can be improved.'.
The improvement calls for adding to our set of commands. Dos
gives us another powerful (and again, not often used) batch file
command. What is it ? Why SHIFT, of course ! Lets rewrite our
batch file, including the SHIFT command.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 24
ECHO OFF
CLS
:DODEL
IF '%1'=='' GOTO EXIT
DEL %1
SHIFT
GOTO DODEL
:EXIT
Quit a bit different ! How does it work ? We'll skip all lines
that we've already discussed, so starting on line 3, we find the
label ':DODEL ', all labels are prefixed by a colon, and are the
only entries on the line. The next line tests for the condition
when the variable %1 is empty. If the variable is empty, we exit.
Now we simply have a command to del the variable %1 (line 5).
Line 6 is probably the key to this whole batch file. The single
expression SHIFT. What this does is cause Dos to point to the
next entered expression from the entered command line. The next
line simply loops thru to label ':DODEL '. We get to the label
':EXIT 'from the 4th line, when %1 is empty.
OK, you've got a question. What is a variable, and what the heck
is %1 ? Dos allows us 10 variables plus the variable %%F as we
used in the previous example. The variables we are allowed to use
are %0 thru %9. We can't use %0, because Dos has reserved that
for a particular use (we will go into that later), which leaves
us 9 variables. Well, what's a variable ? A variable is any of
several passed parameters on a command line when you execute a
program directly from the Dos prompt. IE... in our earlier
example we entered 'D COMMAND.COM FILE.1 TEST.BAT TEST.DAT ' each
entry after the 'D ' is a variable, and Dos assigns them numbers
starting with 1 ! Thus, COMMAND.COM becomes %1, FILE.1 becomes %2
and etc.....
This is where SHIFT comes in. When Shift is encountered, all
variables are shifted down one number, thus, COMMAND.COM is lost,
FILE.1 becomes %1, TEST.BAT becomes %2, etc....
Wow ! Now you see why this is so powerful ! Using this, we can
virtually have an unlimited number of parameters. This of course
isn't true. Dos allows us a line length of up to 127 characters.
What this translates to depends upon the length of each file name
entered on the line. Thus if your command is a single character,
as in 'D', we loose only 2 characters (the required space after
the command), thus giving us 125 bytes for our file list. If each
name averaged 9 bytes (counting extension) then we would have
room for 125 / 9 entries or about 13 entries. The imagination
runs wild ! Think of this capability used with some of those
other Dos commands !
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 25
Now let's discuss the next very powerful command. You will find
many Dos books and computer consultants as well as those who say
they know, that you can NOT call another batch file, without
loading in a second copy of COMMAND.COM . I will now tell you
that is totally untrue, and will precede to show YOU how to do
it. One of the most under used Dos commands is the SET command,
which can be used even from the command line. We've already used
this to show the Dos environment data, such as PATH and COMSPEC .
SET can be used to create any variable ! The only limit to the
name are the legal characters that Dos itself limits us to.
Lets try one from the Dos prompt, just to show you what we can
do. Enter 'SET NAME=Ed' and press enter. Now enter 'set ' and
press enter. You will see the following lines.
PATH=BATCH
COMSPEC=A:\COMMAND.COM
PROMPT=$P$G
NAME=Ed
How about that ! What good is it ? We'll get to that shortly,
but first a word more about the environment and the PATH
statement. At the beginning (somewhere), we mentioned the fact
that Dos sets aside only 160 bytes of space for the environment.
That is true, except for the case we change that, at boot time,
from the CONFIG.SYS file.
MORE ROOM FOR VARIABLES : That's the name of the game, starting
with Dos 3.2, we can change the size of the Dos environment with
the CONFIG command 'SHELL=A:\COMMAND.COM /E:nnn A:\ /P '. This
line (usually the last line), will load COMMAND.COM, with 'nnn'
bytes of environment space, instead of the default 160 bytes.
IE...if you entered 1000 for nnn, then you would have 1000 bytes
available for the environment variables.
Why do we need more room ? You may think at first sight, that
160 bytes is too many, after all, we have only used about 50 or
so bytes. OK, lets take another look at the PATH command.
If you are working with a system which only has floppy drives,
you might want to bypass this section, and start back on page 27.
For hard disk systems, especially those larger than 20 megabytes,
a user should create several directories. IE...your
wordprocessor(s) should be in a directory separate from let's
say, Dos utilities ; Dos utilities should be in a directory
separate from let's say, a graphics program ; and yet another
directory should be created for games, and yet another for Basic,
and yet another for batch files, etc.....
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 26
Why do this ? Well, one reason is that we only have 8 characters
to play with for file names, thus many programs come with
duplicate files, such as README.DOC (often found on Shareware
disks), INSTALL.COM (or EXE) is often a file used to install a
particular program, etc... Let's face it, how many recognizable
words can be generated from words with lengths not to exceed 8
bytes ? Back to the PATH command. We already have one path
defined as BATCH, but for hard disk users, this may not be
enough. IE.. some files you may have to refer to are going to be
DOS commands (the external ones), like FORMAT, DISKCOPY, etc....
Usually these commands are placed in a directory labeled DOS, or
MSDOS, so we would add that path to our PATH directive, which
then becomes PATH=A:\BATCH;C:\MSDOS . Notice the semi-colon
between path names, and that we have used the drive letter C .
The first hard disk in a system is usually C, even if there isn't
a B . Assuming that we have all of the Dos external commands in
the directory MSDOS, what other directory paths might we have to
include ? Well, how about your word processor, you will probably
want to have access to that from else where. So we add the
directory path of C:\EDITORS and come up with the following :
PATH=A:\BATCH;C:\MSDOS;C:\EDITORS
Just those 3 paths use up about 35 bytes. Many systems have path
statements with as many as 8 or 9 different paths. At an average
of 8 bytes per entry, that would represent 64 bytes to 72 bytes.
Then, some programs require a Dos variable be set in the
environment, thus, the bytes add up. We suggest a minimum of 500
bytes be set aside for the environment space, or preferably 1000
if you have the room.
Also, for your information, Dos searches for files in the
following order. First a search is made of the current
directory, then, if the path statement has been set, the search
is conducted starting at the first entry in the PATH variable,
then the second, etc... We suggest that you set your PATH
variable such that those programs or utilities called the most
often are in the first entry.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 27
We can now precede to show how one batch file can call another.
For our discussion, we will use two variables, CALLER and RETURN.
The first variable will be set by the caller to give the name of
the batch file to return to from the called batch file, while the
second one will be where in the batch file to return to. Here's
our example.
Batch file 1, CALLER.BAT
IF '%RETURN%'=='' GOTO STDENTR
GOTO %RETURN%
IF ERRORLEVEL 1 GOTO DOERR
:STDENTR
ECHO OFF
CLS
SET CALLER=CALLER
SET RETURN=WEBACK
CALLED
:WEBACK
ECHO Back from subroutine
GOTO EXIT
:DOERR
ECHO We had an error during execution
:EXIT
Batch file 2, CALLED.BAT
ECHO Subroutine 1
IF '%CALLER%'==''GOTO DOERR
%CALLER%
:DOERR
Echo Subroutine exit failure
:EXIT
Let's see what we did. The first batch file upon running the
first time falls thru to the section starting with the label
:STDENTR , setting the variables CALLER and RETURN , then
executing the subroutine CALLED. CALLED then displays the line
Subroutine 1, and exits back to the calling batch file via the
line %CALLER% . When CALLER is again executed, it finds the
variables RETURN and CALLER set, so will then start executing
starting at the label :WEBACK , which displays the line 'Back
from subroutine'. As you can see, using this approach we not
only can call another batch file and return, but we can return to
any where we want, within the batch file, provided we have
defined a label. With memory a premium in some systems, this
approach saves quite a bit of memory for running programs, even
while doing so from a batch file.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 28
Notice again, the CALLER batch file. Here is a line that uses
the ERRORLEVEL command (function). If the name specified in the
RETURN variable is NOT one of the labels in our batch file, and
Dos tries to goto that line, an errorlevel will be generated. In
this case, an ERRORLEVEL of 1 means failure. If there were no
failure, then execution would precede at the specified label held
in the variable RETURN.
There are still some internal Dos commands we haven't covered,
but those will be covered when we start discussing our own batch
file utilities (SEBFU). Now, we will again refer back to the use
of directories. We feel that proper usage of directories will aid
the user on hard disk systems, and in many cases, those with only
floppy drives.
Other than the fact that there is a good likely hood of file
duplication (file names the same), what other reasons can we give
to use directories. The best reason, is common sense. What do
we mean by that ? Well, if you have one directory with 100 files
in it, how will you find a particular one ? An example, you are
using your word processor, and have created the file TEDDY.LTR ,
so far no problem. Now a week later, you create another file
called TEDDY.LET . Still no problem. OK, a month later you are in
your word processor and need to view and edit your TEDDY letter.
Well, you get a directory list, and there they are, 15 TEDDY's .
Now what ? Each TEDDY is different, but you only remember the
word TEDDY, and you have TEDDY.LTR, TEDDY.LET, TEDDYH.LTR,
TEDDYZ.LET, TEDDY.LT, etc.... Which is the one you want ? Well,
if you are in Dos with the directory command, you can use the DIR
with the /p (for page) option to scroll thru them and look for
the correct date, if you remember it.
The previous was not a common event, more likely, you just won't
remember the name you gave the document or letter. That's where
directories come in. We can create directories for different
types of letters, IE... separate business from personal, create a
directory for documents, one for resume's, etc.... Another
approach would be to have directories named after the months,
IE... JAN, FEB, MAR etc...
What about Dos commands and those nifty utilities (like our batch
file utilities). Where do you put them ? We mentioned earlier,
that the Dos commands like FORMAT, DISKCOPY, etc... could go in a
directory called DOS, but you might not want to put the utilities
there, after all, there may be names that are alike, and if there
are too many, how do you keep track of them ? Another thing, is
the search, you don't want you system tied up searching
constantly for files, so you place those files that are most
often used in one directory, and those less frequently used in
another.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 29
We are including the following only as a recommendation, not as a
must do. For those of you who want fast systems and systems where
you can find something, we suggest the following directories.
In the root directory create a directory called DOS. This will be
where you place all of your MS/PC-DOS utilities (like FORMAT).
Change directories to DOS, and start making subdirectories of DOS
in the following manner. Make directories starting as LEV1, then
LEV2, LEV3, etc... Each level (as in LEV1) specifies where in the
PATH chain to place that directory name. Example :
PATH=C:\DOS\LEV1;C:\DOS\LEV2;C:\DOS\LEV3;etc....
Make only as many levels as you will need (probably 3). We will
base this discussion on 3 levels, as that will suffice for most
users. Level 1 (LEV1) should contain the following files :
DISKCOPY FC FIND FORMAT MORE SORT
XCOPY COMP
Level 2 should contain these files :
ATTRIB CHKDSK DISKCOMP JOIN LABEL MODE
PRINT REPLACE SYS
Level 3 should contain the rest of PC/MS-DOS files.
APPEND ASSIGN BACKUP EXE2BIN FASTOPEN FDISK
GRAFTABL GRAPHICS JOIN KEYB NLSFUNC
RECOVER RESTORE SELECT SHARE SUBST TREE
The above is not written in cast iron, but should only be used as
a starting guide. Some of you will be using GRAFTABL and GRAPHICS
much more often, and should place them in lower levels, others
will be using EXE2BIN more often. All we have attempted to do, is
give you some form of a guide.
Now you will want to change back to the root directory (cd \) and
create a directory called BAT. This directory will contain
commonly used batch files (generic types), like our example of
the delete utility from page 24.
Your root directory should contain only 3 files, COMMAND.COM ,
AUTOEXEC.BAT , and CONFIG.SYS .
Now make a directory called UTILS. This is where you will be
placing our Batch File Utilities (SEBFU) and many more Public
Domain / Shareware utilities (Maybe even the Norton Utilities).
Change to the newly created directory (cd utils).
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 30
We are now going to create directories inside the directory UTILS
The first one will of course be a directory for our utilities
(SEBFU). Make a directory called SEBFU1 . This is where you will
place all of the Batch file utilities that you will use most
often. Examples of often used batch commands are :
LOCATE CLR SCROLL BEEP COFF COLOR
CT CUON DBLBOX DLINE DSKRDY GKEY
INKEY LINE RESPONSE SHOWVAR SNGLBOX SOUND
STRING WAIT WRITE WRITEF YN GETCR
The rest of our utilities will go into a directory called SEBFU2.
In other words, go back one directory level to UTILS, make a
directory called SEBFU2, and copy the rest of our utilities into
the directory. The utilities are :
DAYOFMO DETANSI ENVSIZE FF GDRIVE GETCC
GETMCB GETVER GMEM GMODE INVERT LEN
LPT1TO2 PRDY PRINT PRINTC PRINTF
PRNSET SCROLMSG SETCUR SETDATE SETIME
WHATDAY WHATYR WHATMIN WHATMO WHATYR YRSINCE
Some of the above you may end up putting into the 1st directory
SEBFU1, such as PRDY, LPT1TO2, PRINT, etc... depending on your
use of the printer, or maybe you may use the SCROLMSG a lot, then
that would go into the directory SEBFU1 . Set your new PATH such
that your environment space looks like :
PATH=C:\DOS\LEV1;C:\UTILS\SEBFU1;C:\DOS\LEV2;C:\BAT;
C:\UTILS\SEBFU2;C:\DOS\LEV3
Now you can get a better idea of how fast the environment space
can get used up. The above line is about 85 bytes long ! That 85
bytes plus the bytes necessary for the COMSPEC=C:\COMMAND.COM
would be over a 110 bytes ! That leave less than 50 bytes, if you
are satisfied with the Dos default value of 160 bytes.
The above doesn't include any other paths that you may want to
add to the list, such as one to your wordprocessor, one to the
NORTON UTILITIES, or whatever else you may have. If you are
convinced about adding more space for the environment variables,
then be sure to add the line :
SHELL=C:\COMMAND.COM /E:nnn C:\ /P
The 'nnn' in the above is the number of bytes you want Dos to set
aside for the environment variables. Our suggestion is 1000, or
a minimum of 500, although more or less are ok, depending upon
your needs.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 31
USING SEBFU: Up to this point, we have discussed only the Dos
internal commands. Two of the biggest objections we have to
batch files are the lack of cursor controls and keyboard input.
The foregoing does NOT include the DOS external commands like
FORMAT, which do use user input (but still has no control of the
screen display and cursor). How can keyboard input help ?
Let's use our simplest keyboard input (YN) to demonstrate in a
batch file the use of keyboard responses.
ECHO OFF
:FMTLP
CLS
ECHO Do you really want to Format the disk in Drive A (Y/N) ?
YN
IF ERRORLEVEL 1 GOTO NOFMT
ECHO Formatting disk in drive A
FORMAT A: < ans
ECHO Format complete !
PROMPT $_
PROMPT $P$G
ECHO Format another disk (Y/N) ?
YN
IF ERRORLEVEL 1 GOTO NOFMT
GOTO FMTLP
:NOFMT
ECHO Done !
In the above, we have set up a loop (format loop) starting at
label :FMTLP . In our first ECHO command, we asked the user if
they want to format a disk in drive A . This is done, as a
verify operation, in case the user got here by mistake, thus
allowing the user to exit without formatting a disk. The simple
YN on the next line, executes our utility YN.COM which waits for
the user to press either a Y or N (upper or lower case), and
returns an ERRORLEVEL of 1 if N was pressed, and 0 if the Y was
pressed. The program will NOT exit with other keystrokes, but
instead, will beep if any other key is pressed.
The following line tests the ERRORLEVEL set by YN . If the
ERRORLEVEL is 1 (a no response), then the program will exit via
label :NOFMT . However, if the response were yes (Y) then the
program cycles to the ECHO statement which displays 'Formatting
disk in drive A ', followed by the actual command to format the
disk in drive A (FORMAT A: < c:\bat\ans). Whats the '< ans ' for
? Here, we have gotten a little tricky, and are using one of the
Dos redirection commands to send input from the file ANS which is
on drive C in directory BAT . That file contains these lines :
{blank line}
{blank line}
n
{blank line}
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 32
This pipes the required responses to FORMAT so that the format
will occur without user intervention. The next two lines are used
to generate a CR/LF (a blank line), with the second prompt
command returning the prompt back to our normal value. Then we
display the message 'Format complete !' and ask the user if
another disk needs to be formatted with the string 'Format
another disk (Y/N)' and then getting the Y or N response via our
YN.COM utility. Based on the returned response (ERRORLEVEL), we
either exit via the label :NOFMT or loop thru again via the label
:FMTLP .
Although the above is certainly good, it still can be improved.
Below, is an improved version.
ECHO OFF
:FMTLP
CLS
ECHO Do you really want to Format the disk in Drive A (Y/N) ?
YN
IF ERRORLEVEL 1 GOTO NOFMT
ECHO Formatting disk in drive A
FORMAT A: < ans > NUL
IF ERRORLEVE 1 GOTO FMTFAIL
ECHO Format complete !
PROMPT $_
PROMPT $P$G
ECHO Format another disk (Y/N) ?
YN
IF ERRORLEVE 1 GOTO NOFMT
GOTO FMTLP
:FMTFAIL
ECHO Format failure, check disk and drive.
ECHO Do you wish to retry (Y/N) ?
YN
IF NOT ERRORLEVEL 1 GOTO FMTLP
:NOFMT
ECHO Done !
What have we added ? The line containing the FORMAT command has
an additional redirection command, the '> NUL', which redirects
the output of FORMAT to the NUL device. Whats that ? The null
device is nothing ! That is, we are sending the output nowhere !
This is very useful in circumstances where we don't want to
clutter up the display. This way, we only have the messages we
want on the screen. Also we've added an additional ERRORLEVEL
test, after the FORMAT command, to check for a successful format.
If the format failed (bad disk, no disk, or whatever), the the
program loops to the label :FMTFAIL where we ask the user to
check the situation out and enter a Y if He/She wishes to try
again. Otherwise we will exit via the label :NOFMT . Notice the
ERRORLEVEL test performed after our call to YN . We used another
Dos feature the NOT command. Used as we did, if the ERRORLEVEL is
0, the program loops to :FMTLP and if 1 drops to :NOFMT !
Look what we've been able to do by just adding 1 command !
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 33
Let's add another command. That's one thing that you can't
easily do in other programming languages (add more commands).
Which do we add ? How about LOCATE ? Ok, lets add LOCATE to the
previous example and see what we can do.
ECHO OFF
:FMTLP
CLS
LOCATE 5 5
ECHO Do you really want to Format the disk in Drive A (Y/N) ?
LOCATE 5 63
YN
IF ERRORLEVEL 1 GOTO NOFMT
LOCATE 7 5
ECHO Formatting disk in drive A
FORMAT A: < ans > NUL
IF ERRORLEVE 1 GOTO FMTFAIL
LOCATE 8 5
ECHO Format complete !
LOCATE 10 5
ECHO Format another disk (Y/N) ?
LOCATE 10 33
YN
IF ERRORLEVE 1 GOTO NOFMT
GOTO FMTLP
:FMTFAIL
LOCATE 8 5
ECHO Format failure, check disk and drive.
LOCATE 9 5
ECHO Do you wish to retry (Y/N) ?
LOCATE 9 34
YN
IF NOT ERRORLEVEL 1 GOTO FMTLP
:NOFMT
LOCATE 15 5
ECHO Done !
Actually, we really haven't done a whole lot. What we have done,
is make the actual display of information a little nicer. For
instance, in the previous version, each time we called our YN
utility, the cursor would be on a different line, with the above,
after executing each echo, we reposition the cursor to the end of
the line. Pretty neat ! We can improve all this a little more
without adding any additional commands, by making use of an added
feature of our YN command. YN allow you to enter a string to
display, so using this feature, the batch file now becomes.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 34
ECHO OFF
:FMTLP
CLS
LOCATE 5 5
YN Do you really want to Format the disk in Drive A (Y/N) ?
IF ERRORLEVEL 1 GOTO NOFMT
LOCATE 7 5
ECHO Formatting disk in drive A
FORMAT A: < ans > NUL
IF ERRORLEVE 1 GOTO FMTFAIL
LOCATE 8 5
ECHO Format complete !
LOCATE 10 5
YN Format another disk (Y/N) ?
IF ERRORLEVE 1 GOTO NOFMT
GOTO FMTLP
:FMTFAIL
LOCATE 8 5
ECHO Format failure, check disk and drive.
LOCATE 9 5
YN Do you wish to retry (Y/N) ?
IF NOT ERRORLEVEL 1 GOTO FMTLP
:NOFMT
LOCATE 15 5
ECHO Done !
We've made the batch file significantly shorter. This means,
that it will execute just a little bit faster. Each time we call
a command, it takes our batch file just a little longer to
execute, and also takes up more disk space. Since we are trying
to make this document as generic as possible, we won't go into
the additional feature of adding color. Yes, our YN allows you
to specify which color to use when display the text. If this
feature interests you, we suggest you read our file BATCH.DOC ,
which explains each of the commands in our utility set.
How can we add to this batch file ? Let's add yet another
command, the DSKRDY command. This little baby will set the
ERRORLEVEL based upon the status of the selected drive. The
ERRORLEVEL will be 0 if the drive is ready, and a formatted disk
is installed otherwise, the ERRORLEVEL's returned will be :
ERRORLEVEL = 1 drive not ready (door open)
ERRORLEVEL = 2 disk in drive is NOT formatted
ERRORLEVEL = 3 drive not found
For our demonstration batch file, we really want the condition
where ERRORLEVEL is a 2 (disk in drive NOT formatted). Ideally,
the user will be formatting a NEW disk. For those cases the user
wants to format a used disk, we will prompt for verification.
Here is the batch file redone to make use of the new command :
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 35
ECHO OFF
:FMTLP
CLS
LOCATE 5 5
YN Are you ready to Format the disk in Drive A (Y/N) ?
IF ERRORLEVEL 1 GOTO NOFMT
DSKRDY A:
IF ERRORLEVEL 3 GOTO NOTRDY
IF ERRORLEVEL 2 GOTO FMTIT
IF ERRORLEVEL 1 GOTO NOTRDY
CLS
LOCATE 5 5
ECHO The disk in drive A has data on it, are you sure you
LOCATE 5 53
YN you want to format it (Y/N) ?
IF ERRORLEVEL 1 GOTO NOFMT
:FMTIT
LOCATE 7 5
ECHO Formatting disk in drive A
FORMAT A: < ans > NUL
IF ERRORLEVE 1 GOTO FMTFAIL
LOCATE 8 5
ECHO Format complete !
LOCATE 10 5
YN Format another disk (Y/N) ?
IF ERRORLEVE 1 GOTO NOFMT
GOTO FMTLP
:NOTRDY
LOCATE 8 5
YN Drive not Ready, try again ?
IF ERRORLEVEL 1 GOTO NOFMT
GOTO FMTLP
:FMTFAIL
LOCATE 8 5
ECHO Format failure, check disk and drive.
LOCATE 9 5
YN Do you wish to retry (Y/N) ?
IF NOT ERRORLEVEL 1 GOTO FMTLP
:NOFMT
LOCATE 15 5
ECHO Done !
Well, how about that, we sure have enhanced this little utility.
Add this to your library of batch files, and never worry again
about accidentally formatting a disk with data on it ! But, guess
what, we are even going say, that this still is NOT enough. What
can we do to make this even better ? Easy, let's make this
utility allow the user to select which drive to format ! We will
do this by adding the command GKEY to our growing list of
commands.
Let's go ahead and rewrite the utility, including the ability to
format either drive A or B .
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 36
ECHO OFF
IF '%1'=='' GOTO GETDRV
CLS
IF %1==A GOTO FMTLPA
IF %1==a GOTO FMTLPA
IF %1==B GOTO FMTLPB
IF NOT %1==b GOTO GETDRV
:FMTLPB
LOCATE 5 5
YN Are you ready to Format the disk in Drive B (Y/N) ?
IF ERRORLEVEL 1 GOTO NOFMT
DSKRDY B:
IF ERRORLEVEL 3 GOTO NODRV
IF ERRORLEVEL 2 GOTO FMTIT
IF ERRORLEVEL 1 GOTO NOTRDY
CLS
LOCATE 5 5
ECHO The disk in drive B has data on it, are you sure you
LOCATE 5 53
YN you want to format it (Y/N) ?
IF ERRORLEVEL 1 GOTO NOFMT
:FMTITB
LOCATE 7 5
ECHO Formatting disk in drive B
FORMAT B: < c:\bat\ans > NUL {you may need to change drive}
GOTO EFMT
:FMTLPA
LOCATE 5 5
YN Are you ready to Format the disk in Drive A (Y/N) ?
IF ERRORLEVEL 1 GOTO NOFMT
DSKRDY A:
IF ERRORLEVEL 3 GOTO NODRV
IF ERRORLEVEL 2 GOTO FMTITA
IF ERRORLEVEL 1 GOTO NOTRDY
CLS
LOCATE 5 5
ECHO The disk in drive A has data on it, are you sure you
LOCATE 5 53
YN you want to format it (Y/N) ?
IF ERRORLEVEL 1 GOTO NOFMT
:FMTITA
LOCATE 7 5
ECHO Formatting disk in drive A
FORMAT A: < c:\bat\ans > NUL {you may need to change drive}
:EFMT
IF ERRORLEVE 1 GOTO FMTFAIL
LOCATE 8 5
ECHO Format complete !
LOCATE 10 5
YN Format another disk (Y/N) ?
IF ERRORLEVE 1 GOTO NOFMT
GOTO FMTLP
{continues on next page}
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 37
{continued from page 36}
:NODRV
LOCATE 8 5
ECHO The drive you selected to format does not exist,
LOCATE 9 5
ECHO Please select the other drive.
:GETDRV
LOCATE 12 5
ECHO Enter a (1) to format drive A or a (2) to format drive B
LOCATE 13 5
ECHO Your choice (1 or 2 or 0 to abort) ?
LOCATE 13 23
GKEY 012
IF ERRORLEVEL 2 GOTO FMTLPB
IF ERRORLEVEL 1 GOTO FMTLPA
GOTO NOFMT
:NOTRDY
LOCATE 8 5
YN Drive not Ready, try again ?
IF ERRORLEVEL 1 GOTO NOFMT
GOTO FMTLP
:FMTFAIL
LOCATE 8 5
ECHO Format failure, check disk and drive.
LOCATE 9 5
YN Do you wish to retry (Y/N) ?
IF NOT ERRORLEVEL 1 GOTO FMTLP
:NOFMT
LOCATE 15 5
ECHO Done !
You can try this great new utility, and you won't even have to
type it in. We have placed this batch file on this disk, and we
call it FMT.BAT . To use, just enter FMT d where d is either A
or B (upper or lower case), or just enter FMT, and you will be
prompted for the drive. How's that for a neat utility ? NOTICE :
there are 2 lines above that may need to be edited, they are the
lines with the string '{you may have to change drive}' appended
at the ends. What this means is, for instance, you may have these
utilities on drive A.
For those of you who have single drive machines, we haven't
forgotten you. This disk also contains a batch file called
SFMT.BAT , which is a single disk version of the utility. This
version won't ask you which drive to format, but will detect if
you have a previously formatted disk installed.
We suggest to all of you (especially those with only floppy
drives) to place these utilities on a Ram Disk while running the
utility. If you do so, the utility will run a lot faster, even on
hard disk systems.
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 38
Here is a short batch file to use prior to executing ether
FMT.BAT or SFMT.BAT , for those of you with a Ram Disk installed.
ECHO OFF
CLS
LOCATE 5 5
ECHO This utility will copy the batch file FMT or SFMT to your
LOCATE 6 5
ECHO Ram disk, along with all of the necessary Batch File Utils
LOCATE 7 5
ECHO required by the batch file.
LOCATE 9 5
ECHO You must be logged onto the drive and directory where the
LOCATE 10 5
ECHO utilities are prior to running this.
LOCATE 12 5
YN Are you running a single drive system (Y/N) ?
IF ERRORLEVEL 1 GOTO INSTFMT
:INSTSFMT
CLS
LOCATE 5 5
YN Are you ready for the transfer ?
IF ERRORLEVEL 1 GOTO NODO
MD D:BAT
IF ERRORLEVEL 1 GOTO NODSK
CD D:BAT
IF NOT EXIST SFMTD.BAT GOTO NOTFND
COPY SFMT.BAT D:FMT.BAT
GOTO TREST
:INSTFMT
CLS
YN Are you ready for the transfer
IF ERRORLEVEL 1 GOTO EXIT
MD D:BAT
IF ERRORLEVEL 1 GOTO NODSK
CD D:BAT
IF NOT EXIST FMTD.BAT GOTO NOTFND
COPY FMT.BAT D:FMT.BAT
:TREST
IF NOT EXIST LOCATE.COM GOTO NOFND
COPY LOCATE.COM D: {We assume your Ram disk to
IF NOT EXIST YN.COM GOTO NOFND be drive D, if this is not
COPY YN.COM D: the case, change the D to
IF NOT EXIST GKEY.COM GOTO NOFND whatever drive letter you
COPY GKEY.COM D: your Ram disk is}
IF NOT EXIST DSKRDY.COM GOTO NOFND
COPY DSKRDY.COM D:
IF NOT EXIST ANS GOTO NOFND
COPY ANS D:
:GOTALL
LOCATE 7 5
ECHO All files transferred !
GOTO EXIT
{continues next page}
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 39
{continued from page 38}
:NOTFND
LOCATE 7 5
ECHO Files were NOT transferred !
GOTO EXIT
:NODSK
ECHO Ram disk NOT found !
GOTO NOTFND
:EXIT
Notice : We actually have a batch file (2 actually) for use on
Ram disks, they are SFMTD and FMTD , one for single drive systems
and one for 2 drive systems. By the way, you need NOT enter the
above batch file, it's also on your disk. The batch file name is
XFRAM.BAT . To use, just log onto our disk and enter XFRAM at the
Dos prompt.
Here's a test for you, look up our batch file commands BEEP, CUON
and COFF. Add these to the last 2 batch files, and try to improve
them. If you do improve the above 2 utilities, and send them to
us, and we add them to our utility disk, we will send you the
upgrade FREE, and you will get credit (in the documentation) for
making the batch files. You might say, that this is like a
contest. Good Luck !
Let's add a new command to our list of commands. WRITE was
created to enhance displaying text on the display. With WRITE, it
now becomes easy to display a blank line, to merge 2 (or more)
lines together (to form one long line) and for those with color
monitors, to add a little color to their life.
We already have 2 great utilities, so why not another ? Many of
you need to copy files from disk to disk, so let's make a Wild
copy utility. We'll start off by giving an over view of what we
expect it to do, then we will show the listing and finally
explain it in a line by line basis.
This utility will have what is called a command line, by which we
mean, that you will enter parameters after entering the name of
the batch file (on the same line), and that the batch file will
act upon those parameters. This utility will be for those with 2
drives (A and B), but it may be modified for different
combinations, or even to work across several drives, such as a
hard disk and 2 floppy drives. This batch file will allow you to
pass it several parameters, with the 1st parameter specifying
which drive to copy from. If for instance, you enter the batch
file with A as the first parameter, it will be assumed that you
want to copy the files to drive B . We will call this file
CPY.BAT . Thus, entering the line :
CPY A TEST.BAT TEST.DAT LOCATE.COM GKEY.COM YN.COM WRITE.COM
Would copy the files TEST.BAT, TEST.DAT, LOCATE.COM, GKEY.COM,
YN.COM and WRITE.COM from drive A to drive B .
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 40
{CPY.BAT}
ECHO OFF
CLS
IF '%1'=='' GOTO BADXIT {test if drive was selected}
IF %1==A SET SOURCE=A {source = A ?}
IF %1==a SET SOURCE=A
IF %1==B SET SOURCE=B {source = B ?}
IF NOT %1==b GOTO BADXIT
IF %SOURCE%==A SET DEST=B {set destination based on
IF %SOURCE%==B SET DEST=A source}
LOCATE 5 5
WRITE ~~Transfer files from
IF %DEST%==A WRITE ~~B
IF %DEST%==B WRITE ~~A
WRITE ~~to
IF %DEST%==A WRITE ~~A
IF %DEST%==B WRITE ~~B
YN , correct ?
IF ERRORLEVEL 1 GOTO EXIT
CLS
LOCATE 5 5
WRITE Transfer started.
WRITE
SHIFT
SHIFT
:CPYLP
SHIFT
IF '%1'=='' GOTO DONE
COPY %SOURCE%:%1 %DEST% > NUL {don't show anything
IF ERRORLEVEL 1 GOTO CPYERR
WRITE ~~Transferred
ECHO %1
GOTO CPYLP
:DONE
WRITE
WRITE Transfer completed
GOTO EXIT
:CPYERR
WRITE
WRITE Transfer process failure.
WRITE
WRITE ~~Problem occurred during Copy of
ECHO %1
GOTO EXIT
:BADXIT
WRITE
WRITE Transfer utility aborted, bad entry source drive.
:EXIT
SET SOURCE=
SET DEST=
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 41
Another challenge ! Make a version of this with more bells and
whistles, and allow for more than the above source and
destinations. Send us your entry, if chosen, your entry will be
included in our next release of SEBFU, and you will receive a
FREE upgrade.
OK, how does this work ? We start off by setting the 2 variables
DEST and SOURCE to equal the destination and source drives. If
the passed source drive is NOT valid, we just exit. Next, we
display a message to verify that the source and destinations are
correct. If the user responds to the YN function with a N we exit
the program without any other fanfare. If the answer is yes, then
we precede with the copying process.
We pass each file name, as we find them, to the COPY command via
the variable %1, while specifying both the destination and source
via the two variables SOURCE and DEST . We loop thru label CPYLP
until we don't find a parameter, announcing a successful copy
process, or exiting with an error message via label CPYERR .
Possible improvements include prompting for the source and
destination, allowing a different source and destination for each
occurrence, allowing a switch for verification, proceeding to the
next entry even if a failure occurs. All of these enhancements
can be made, and should none of you write in a good version for
us, we'll write one our selves.
To aid in speeding up screen displays, we have created WRITEF,
which will display an entire file on the display device. Why
would we do that ? Each time you make a call to Dos to execute a
command, a file must be read in, then the program must interpret
the command line (if there is one), then perform the assigned
task. Our single line WRITE command, although very useful, has
the same disadvantage as the Dos ECHO command, in that for each
call to it, WRITE must be read into memory, probably from a disk,
then it must perform its task. Usually, we could use the TYPE
command to display a file, but TYPE has some draw-backs as well.
One of the disadvantages to using TYPE is that all lines are left
justified, and if you have ANSI.SYS installed, you are allowed to
use only the existing set color. If you doubt it, use our COLOR
command to set the color to, say green on black, then use our CLR
command to clear the display to white on blue. Next position the
cursor to the top of the display, and use the TYPE command to
display a short file. If ANSI.SYS is installed, the file will be
displayed in green on black, and if it's not, the file will be
displayed in white on blue. This can easily be done using the
following lines in a batch file.
ECHO OFF
COLOR 0 2
CLR 1 1 25 80
LOCATE 1 1
TYPE {file} enter the file name you want to display
B A T C H F I L E T U T O R Ver. 1.7 (SEBFT)
by Scanlon Enterprises Page 42
Now try the same thing with our WRITEF command, substituting
WRITE for the TYPE in the above lines. WRITEF also allows you to
set the left margin. To do this, use our LOCATE command. In the
above, substitute LOCATE 1 5 for the line LOCATE 1 1, and watch
the results. Each line will now start on column 5.
What do you do when you want to display a character, that you
just can't get your word processor to let you enter ? We've fixed
that possibility by supplying you with the command STRING, which
not only allows you to display any character in the range of 0
thru 255, but also to display multiple values of that character,
in what ever color you want. By using the /P switch, the
character can also be printer.
Want to make music, or just some noise, well, we give you SOUND.
This will sound the speaker for a specified period of time and at
a specified tone, similar to the BASIC SOUND statement. This can
be used to make a noise announcing the end of a command cycle,
announce errors or just about anything. For those of you using
the printer in conjunction with batch files, we have supplied you
with PRDY, which sets an ERRORLEVEL based upon the printer
status, IE.... and ERRORLEVEL of 0 means everything is OK.
Now, what else have we got in our little bag of tricks ? Well,
we have the PRINT command, which does for printing what WRITE
does for the display (no color), we have PRINTF, which prints an
entire file, PRINTC which prints any ascii character, great for
sending printer set up information. There are many more
commands, we'd like you to read the Doc file BATCH.DOC. Don't
forget to send in those suggestions.
Well, we hope that this tutorial has been of help and was
informative. If you like, why not run our demo ? There are 2
versions of the demo, DEMOM for monochrome systems and DEMOC for
color systems. Just enter DEMOM or DEMOC at the Dos prompt. The
color version is a little more flashy, due to the extensive use
of colors, but both demonstrate our utilities very well.
Anyone creating a unique or interesting batch file using our
utilities may send them to us, and if found to be worthy, will be
placed in our batch file library to be included in a future
release and will have their names included in our documentation.
Thank you for using the Batch File Utilities by :
Scanlon Enterprises
38354 17th St. E #C
Palmdale, CA 93550